package org.dallasrowing.logbook.ui.view;

import org.dallasrowing.model.Rower;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class SwtRowerEditorWidget extends SwtBeanEditorWidgetBase<Rower> {

	private Text name;
	private Text email;
	private Text phone;
	private Combo level;
	private Button member;
	private Button active;

	public SwtRowerEditorWidget(Composite parent) {
		super(parent, SWT.NONE);
	}

	protected void createBeanFields() {
		Group g = new Group(this, SWT.BORDER);
		g.setLayout(new GridLayout(2, false));
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		g.setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Name:");
		name = new Text(g, SWT.BORDER);
		name.setLayoutData(gd);
		
		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Email:");
		email = new Text(g, SWT.BORDER);
		email.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(g, SWT.NONE).setText("Phone:");
		phone = new Text(g, SWT.BORDER);
		phone.setLayoutData(gd);

		new Label(g, SWT.NONE).setText("Level:");
		level = new Combo(g, SWT.None);
		level.add("3");
		level.add("4");
		level.add("5");

		new Label(g, SWT.NONE).setText("Is Member:");
		member = new Button(g, SWT.CHECK);

		new Label(g, SWT.NONE).setText("Is Active:");
		active = new Button(g, SWT.CHECK);
	}

	protected void enableEdit() {
		name.setEnabled(true);
		name.setText("");
		email.setEnabled(true);
		email.setText("");
		phone.setEnabled(true);
		phone.setText("");
		level.setEnabled(true);
		level.select(-1);
		member.setEnabled(true);
		member.setSelection(false);
		active.setEnabled(true);
		active.setSelection(false);
	}

	protected void disableEdit() {
		name.setEnabled(false);
		email.setEnabled(false);
		phone.setEnabled(false);
		level.setEnabled(false);
		member.setEnabled(false);
		active.setEnabled(false);
	}

	public void updateBean(Rower bean) {
		bean.setActive(active.getSelection());
		bean.setName(name.getText());
		bean.setPhone(phone.getText());
		bean.setEmail(email.getText());
		bean.setMember(member.getSelection());
		bean.setLevel(Integer.valueOf(level.getItem(level.getSelectionIndex())));
	}

	public void setBean(Rower bean) {
		name.setText(bean.getName());
		email.setText(bean.getEmail());
		phone.setText(bean.getPhone());
		active.setSelection(bean.getActive());
		member.setSelection(bean.isMember());
		level.select(bean.getLevel()-3);
	}

	@Override
	protected String getBeanName(Rower bean) {
		return bean.getName();
	}
}
